package net.easyjava.tools.file;



import net.easyjava.tools.BaseTools;
import net.easyjava.tools.lang.StringTools;

import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by wenxiaoning on 15/11/26.
 */
public class FileTools extends BaseTools {
    private static final long serialVersionUID = 2053928080760806043L;
    public static final String SEPARATOR = FileSystems.getDefault().getSeparator();//系统目录间隔

    /**
     * read file by filePath to List<String>
     * @param filePath
     * @return
     * @throws IOException
     */
    public static List<String> readLine(String filePath) throws IOException{
        Path path = Paths.get(filePath);
        Charset charset = Charset.defaultCharset();
        List<String> list = new ArrayList<String>();
        try(BufferedReader br = Files.newBufferedReader(path,charset)) {
            String content = null;
            while((content = br.readLine())!= null){
                list.add(content);
            }
            return list;
        }catch (IOException e){
            throw new IOException(e);
        }
    }

    public static String readFile(String filePath) throws IOException{
        Path path = Paths.get(filePath);
        Charset charset = Charset.defaultCharset();
        StringBuffer sb = new StringBuffer();
        try(BufferedReader br = Files.newBufferedReader(path,charset)) {
            String content = null;
            while((content = br.readLine())!= null){
                sb.append(content.trim());
            }
            return sb.toString();
        }catch (IOException e){
            throw new IOException(e);
        }
    }


    /**
     * read file from filePath in InputStream
     * @param filePath
     * @return
     * @throws IOException
     */
    public static InputStream read(String filePath) throws IOException{
        Path path = Paths.get(filePath);
        if(Files.notExists(path)){
            throw new FileNotFoundException(filePath+" must exist");
        }
        InputStream is = Files.newInputStream(path,StandardOpenOption.READ);
        BufferedInputStream bis = new BufferedInputStream(is);
        return bis;

    }

    /**
     * write filePath by InputStream
     * @param filePath
     * @param is
     * @throws IOException
     */
    public static void write(String filePath,InputStream is) throws IOException{
        Path path = Paths.get(filePath);
        if(!Files.exists(path)){
            Files.createDirectory(Paths.get(filePath.substring(0,filePath.lastIndexOf(SEPARATOR))));
        }
        try(OutputStream os = Files.newOutputStream(path, StandardOpenOption.CREATE);
            BufferedOutputStream bos = new BufferedOutputStream(os);
        ){

            byte[] bytes = new byte[1024];
            while((is.read(bytes))>0){
                bos.write(bytes);
            }
        }catch (IOException e){
            throw new IOException(e);
        }
    }


    /**
     * write String to filePath
     * @param filePath
     * @param content
     * @throws IOException
     */
    public static void write(String filePath,String content) throws IOException {

        Path path = Paths.get(filePath);
        if(!Files.exists(path)){
//            Files.createDirectory(Paths.get(filePath.substring(0,filePath.lastIndexOf(SEPARATOR))));
            Files.createFile(path);
        }
//        File file = new File(filePath);
//        if (!file.exists()) {
//            file.createNewFile();
//        }
        Charset charset = Charset.defaultCharset();
        try(OutputStream os = Files.newOutputStream(path,StandardOpenOption.CREATE);
            OutputStreamWriter osw = new OutputStreamWriter(os,charset);
            PrintWriter pw = new PrintWriter(new BufferedWriter(osw));) {
            pw.write(content);
        }catch (IOException e){
            throw new IOException(e);
        }

    }

    /**
     * copy sourceFile to targetFile
     * @param sourceFile
     * @param targetFile
     * @throws IOException
     */
    public static void copyFile(String sourceFile,String targetFile) throws IOException {
        Path source = Paths.get(sourceFile);
        Path target = Paths.get(targetFile);
        if(Files.notExists(source)){
            throw new FileNotFoundException(sourceFile+" must exist");
        }

        try(InputStream is = Files.newInputStream(source,StandardOpenOption.READ);
            BufferedInputStream bis = new BufferedInputStream(is);
            OutputStream os = Files.newOutputStream(target, StandardOpenOption.CREATE);
            BufferedOutputStream bos = new BufferedOutputStream(os);) {

            byte[] bytes = new byte[1024];
            while((bis.read(bytes))>0){
                bos.write(bytes);
            }
        }catch (IOException e){
            throw new IOException(e);
        }
    }


    public static void main(String[] args) {
        try {
//            copyFile("/Users/wenxiaoning/Desktop/data/writ.txt","/Users/wenxiaoning/Desktop/data/write2.txt");
//            System.out.println("readLine(\"/Users/wenxiaoning/Desktop/data/write2.txt\") = " + readLine("/Users/wenxiaoning/Desktop/data/write2.txt"));
//            List<String> list = readLine("/Users/wenxiaoning/Desktop/data/write2.txt");
//            for(String str:list){
//                System.out.println("str = " + str);
//            }

            String path = "/Users/wenxiaoning/Desktop/data/easyjava/article/b/j/1451217795968.txt";//"/Users/wenxiaoning/Desktop/data/"+ StringTools.getRandom(1)+"/wwww.txt";
//            FileTools.write(path,"www");
//            FileSystem fileSystem = FileSystems.getDefault();
//            System.out.println("fileSystem = " + fileSystem.getSeparator());


            String[] paths = path.split(SEPARATOR);
            for(String pathName:paths){
                System.out.println("pathName = " + pathName);
                System.out.println("path.substring(0,path.indexOf(pathName)) = " + path.substring(0, path.indexOf(pathName)));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
